/**
 * 工具类
 */
// import Vue from 'vue'
import CryptoJS from "crypto-js";
const emailRegexp = /^([0-9a-zA-Z_\.\-\])+\@([0-9a-zA-Z_\.\-\])+\.([a-zA-Z]+)$/,
  defaultKey = "Eversec123!@#456";
function decrypt(word, keyStr = defaultKey) {
  const key = CryptoJS.enc.Utf8.parse(keyStr), //Latin1 w8m31+Yy/Nw6thPsMpO5fg==
    decrypt = CryptoJS.AES.decrypt(word, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
  return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
export default {
  // 加密
  encrypt(word, keyStr = defaultKey) {
    const key = CryptoJS.enc.Utf8.parse(keyStr), //Latin1 w8m31+Yy/Nw6thPsMpO5fg==
      srcs = CryptoJS.enc.Utf8.parse(word),
      encrypted = CryptoJS.AES.encrypt(srcs, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
      });
    return encrypted.toString();
  },
  // 解密
  decrypt,
  /**
   * 兼容旧数据解密邮件地址
   * @param {string} email 待解密的邮件地址
   * @param {string} keyStr 密钥（可选）
   * @returns string
   */
  decryptEmail(email, keyStr = defaultKey) {
    if (emailRegexp.test(email)) {
      return email.toString();
    }
    return decrypt(email, keyStr);
  }
};
